package demo_09_28_07;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

/**
 * @author:连志鹏
 * @className:work_01
 * @descrption:
 * @date: 2021/9/27 19:31
 * @version: 0.1
 * @since: 1.8
 */
public class work_01 {
    public static void main(String[] args) throws ParseException {
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎使用迷你共享单车系统");
        SharedBikeMgr bikeMgr = new SharedBikeMgr();
        int option = 0;
        bikeMgr.initial();

        while (true)
        {
            menu();
            System.out.print("请输入操作序号：");
            option = sc.nextInt();
            switch (option){
                case 1:
                    bikeMgr.putBike();
                    break;
                case 2:
                    bikeMgr.selectAllBike();
                    break;
                case 3:
                    bikeMgr.deleteBike();
                    break;
                case 4:
                    bikeMgr.lendBike();
                    break;
                case 5:
                    bikeMgr.returnBike();
                    break;
                case 6:
                    break;
                case 7:
                    return;

            }
        }


    }
    public static void menu()
    {
        System.out.println("********************************");
        System.out.println("          1.投放Bike            ");
        System.out.println("          2.查看Bike            ");
        System.out.println("          3.删除Bike            ");
        System.out.println("          4.借出Bike            ");
        System.out.println("          5.归还Bike            ");
        System.out.println("          6.Bike排行榜          ");
        System.out.println("          7.退出                ");
        System.out.println("********************************");
    }
}
class SharedBikeMgr extends Object{

    ArrayList<SharedBikeCompany> list = new ArrayList<SharedBikeCompany>();

    Scanner sc = new Scanner(System.in);


    public void returnBike()throws ParseException
    {
        int ret = 0; int num = 0;
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getBikeCompanyId()+":"+list.get(i).getBikeCompanyName());
        }
        System.out.print("请选择要操作的单车品牌");
        ret = sc.nextInt();
        System.out.println("此单车有共享单车如下：");
        System.out.println("单车序号：| 单车品牌：| 单车状态：| 借出时间");
        for (int j = 0; j < list.get(ret-1).getSharedBikes().length; j++) {
            System.out.print("   "+list.get(ret-1).getSharedBikes()[j].getBid()+
                    "        "+list.get(ret-1).getSharedBikes()[j].getBname()+"        "
            );
            if(list.get(ret-1).getSharedBikes()[j].getStatus()==1)
                System.out.println("可借"+"      ");
            else
                System.out.println("不可借"+"      "+list.get(ret-1).getSharedBikes()[j].getBorrowTime());

        }

        System.out.print("请选择要归还的单车序号：");
        num = sc.nextInt();
        if(list.get(ret-1).getSharedBikes()[num].getStatus()==1)
        {
            System.out.println("此单车未被借出");
            return;
        }

        System.out.println("请输入归还日期(2019-01-01 14:30:30)");

        String startTime = list.get(ret-1).getSharedBikes()[num].getBorrowTime();

        sc.nextLine();
        String endTime = sc.nextLine();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date startDate = dateFormat.parse(startTime);
        Date endDate = dateFormat.parse(endTime.toString());
        Integer datePoor = getDatePoor(startDate,endDate);
        BigDecimal b = new BigDecimal((double)datePoor/60);
        Double hour = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();//第一个参数是保留小数的位数


        double cash = hour*2;
        System.out.println("您的借车时间为:"+startTime);
        System.out.println("您借车"+hour+"小时"+"需付款："+cash+"元");

        list.get(ret-1).getSharedBikes()[num].setBorrowTime(null);
        list.get(ret-1).getSharedBikes()[num].setStatus(1);
        list.get(ret-1).setCount(list.get(ret-1).getCount()+1);
        System.out.println("归还成功！");
    }

    public void lendBike() throws ParseException {
        int ret = 0; int num = 0;
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getBikeCompanyId()+":"+list.get(i).getBikeCompanyName());
        }
        System.out.print("请选择要操作的单车品牌");
        ret = sc.nextInt();
        System.out.println("此单车有共享单车如下：");
        System.out.println("单车序号：| 单车品牌：| 单车状态：| 借出时间");
        for (int j = 0; j < list.get(ret-1).getSharedBikes().length; j++) {
            System.out.print("   "+list.get(ret-1).getSharedBikes()[j].getBid()+
                    "         "+list.get(ret-1).getSharedBikes()[j].getBname()+"        "
            );
            if(list.get(ret-1).getSharedBikes()[j].getStatus()==1)
                System.out.println("可借"+"      ");
            else
                System.out.println("不可借"+"      "+list.get(ret-1).getSharedBikes()[j].getBorrowTime());

        }

        System.out.print("请选择要借出的单车序号：");
        num = sc.nextInt();
        if(list.get(ret-1).getSharedBikes()[num].getStatus()==0)
        {
            System.out.println("此单车已经被借出");
            return;
        }

        System.out.println("请输入借出日期(2019-01-01 12:30:30)");

        String startTime = null;
        sc.nextLine();
        startTime = sc.nextLine();

//      String endTime = "2019-11-08 17:23:00";

        list.get(ret-1).getSharedBikes()[num].setBorrowTime(startTime);
        list.get(ret-1).getSharedBikes()[num].setStatus(0);
        System.out.println("借出成功！");

    }

    public Integer getDatePoor(Date startDate, Date endDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        Long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        Long mymint = diff/1000/60;
        int intValue = mymint.intValue();
        return intValue;
    }

    public void deleteBike()
    {
        int ret = 0; int num = 0;
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getBikeCompanyId()+":"+list.get(i).getBikeCompanyName());
        }
        System.out.print("请选择要操作的单车品牌");
        ret = sc.nextInt();
        System.out.println("此单车有共享单车如下：");
        System.out.println("单车序号：| 单车品牌：| 单车状态：| 借出时间");
        for (int j = 0; j < list.get(ret-1).getSharedBikes().length; j++) {
            System.out.print("   "+list.get(ret-1).getSharedBikes()[j].getBid()+
                    "         "+list.get(ret-1).getSharedBikes()[j].getBname()+"        "
            );
            if(list.get(ret-1).getSharedBikes()[j].getStatus()==1)
                System.out.println("可借"+"        ");
            else
                System.out.println("不可借"+"      "+list.get(ret-1).getSharedBikes()[j].getBorrowTime());

        }

        System.out.print("请选择要删除的单车序号：");
        num = sc.nextInt();
        if(list.get(ret-1).getSharedBikes()[num].getStatus()==0)
        {
            System.out.println("此单车已经被借出");
            return;
        }

        SharedBike[] newbikes = new SharedBike[list.get(ret-1).getSharedBikes().length-1];
        for (int i = 0; i < newbikes.length; i++) {
            newbikes[i] = new SharedBike();
        }
        int n = 0;
        for (int i = 0; i < list.get(ret-1).getSharedBikes().length; i++) {
            if(i!=num)
            {
                newbikes[n].setStatus(list.get(ret-1).getSharedBikes()[i].getStatus());
                newbikes[n].setBorrowTime(list.get(ret-1).getSharedBikes()[i].getBorrowTime());
                newbikes[n].setBname(list.get(ret-1).getSharedBikes()[i].getBname());
                newbikes[n].setBid(n);
                n++;
            }
        }
        list.get(ret-1).setSum(list.get(ret-1).getSum()-1);
        list.get(ret-1).setSharedBikes(newbikes);
        System.out.println("删除成功！");

    }


    public void putBike()
    {
        int ret = 0; int num = 0;
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getBikeCompanyId()+":"+list.get(i).getBikeCompanyName());
        }
        System.out.print("请选择要投放的单车品牌");
        ret = sc.nextInt();
        System.out.print("请选择要投放的数量：");
        num = sc.nextInt();

        SharedBike[] bikes = new SharedBike[num];
        for (int i = 0; i <bikes.length ; i++) {
            bikes[i] = new SharedBike(list.get(ret-1).getSharedBikes().length+i,
                    list.get(ret-1).getBikeCompanyName(),1,null);
        }
        SharedBike[] newbikes = new SharedBike[list.get(ret-1).getSharedBikes().length+num];
        for (int i = 0; i < newbikes.length; i++) {
            newbikes[i] = new SharedBike();
        }
        
        System.arraycopy(list.get(ret-1).getSharedBikes(),
                0,newbikes,0,list.get(ret-1).getSharedBikes().length);

        for (int i = list.get(ret-1).getSharedBikes().length; i < newbikes.length; i++) {
            newbikes[i].setStatus(bikes[i-list.get(ret-1).getSharedBikes().length].getStatus());
            newbikes[i].setBorrowTime(bikes[i-list.get(ret-1).getSharedBikes().length].getBorrowTime());
            newbikes[i].setBname(bikes[i-list.get(ret-1).getSharedBikes().length].getBname());
            newbikes[i].setBid(i);
        }
        list.get(ret-1).setSum(list.get(ret-1).getSum()+num);
        list.get(ret-1).setSharedBikes(newbikes);
        System.out.println("添加成功！");

    }






    public void selectAllBike()
    {

        for (int i = 0; i < list.size(); i++) {
            System.out.println("公司序号：| 公司名称：| 公司单车数量| 公司单车借出次数");
            System.out.println("   "+list.get(i).getBikeCompanyId()+
            "         "+" "+list.get(i).getBikeCompanyName()+"         "+
                    list.get(i).getSum()+"              "+list.get(i).getCount());
            System.out.println("单车序号：| 单车品牌：| 单车状态：| 借出时间");
            for (int j = 0; j < list.get(i).getSharedBikes().length; j++) {


                System.out.print("   "+list.get(i).getSharedBikes()[j].getBid()+
                "         "+list.get(i).getSharedBikes()[j].getBname()+"        "
                );
                if(list.get(i).getSharedBikes()[j].getStatus()==1)
                    System.out.println("可借"+"       ");
                else
                    System.out.println("不可借"+"      "+list.get(i).getSharedBikes()[j].getBorrowTime());

            }
        }
    }



    public void initial()
    {
        SharedBikeCompany company1 = new SharedBikeCompany();
        company1.setBikeCompanyId(1);
        company1.setBikeCompanyName("ofo");
        SharedBike bike = new SharedBike();
        bike.setStatus(1);
        SharedBike[] bikes = new SharedBike[10];

        for (int i = 0; i < bikes.length; i++) {
           bikes[i] = new SharedBike();
           bikes[i].setBid(i);
           bikes[i].setBname(company1.getBikeCompanyName());
           bikes[i].setStatus(1);
           bikes[i].setBorrowTime(null);
        }
        company1.setSharedBikes(bikes);
        company1.setSum(company1.getSharedBikes().length);
        company1.setCount(0);
        this.list.add(company1);

        SharedBikeCompany company2 = new SharedBikeCompany();
        company2.setBikeCompanyId(2);
        company2.setBikeCompanyName("哈罗");
        SharedBike[] bikes1 = new SharedBike[5];
        for (int i = 0; i < bikes1.length; i++) {
            bikes1[i] = new SharedBike();
            bikes1[i].setBname(company2.getBikeCompanyName());
            bikes1[i].setBid(i);
            bikes1[i].setStatus(1);
            bikes1[i].setBorrowTime(null);

        }
        company2.setSharedBikes(bikes1);
        company2.setSum(company2.getSharedBikes().length);
        company2.setCount(0);
        this.list.add(company2);

        SharedBikeCompany company3 = new SharedBikeCompany();
        company3.setBikeCompanyId(3);
        company3.setBikeCompanyName("摩拜");
        SharedBike[] bikes2 = new SharedBike[6];
        for (int i = 0; i < bikes2.length; i++) {
            bikes2[i] = new SharedBike();
            bikes2[i].setBname(company3.getBikeCompanyName());
            bikes2[i].setStatus(1);bikes2[i].setBid(i);
            bikes2[i].setBorrowTime(null);

        }
        company3.setSharedBikes(bikes2);
        company3.setSum(company3.getSharedBikes().length);
        company3.setCount(0);
        this.list.add(company3);
    }


}
class SharedBikeCompany {

     public SharedBikeCompany(){}
     public SharedBikeCompany(Integer bikeCompanyId,
                              String bikeCompanyName,
                              SharedBike[] sharedBikes,
                              Integer sum, Integer count) {
         this.bikeCompanyId = bikeCompanyId;
         this.bikeCompanyName = bikeCompanyName;
         this.sharedBikes = sharedBikes;
         this.sum = sum;
         this.count = count;
     }

     public Integer getBikeCompanyId() {
         return bikeCompanyId;
     }

     public void setBikeCompanyId(Integer bikeCompanyId) {
         this.bikeCompanyId = bikeCompanyId;
     }

     public String getBikeCompanyName() {
         return bikeCompanyName;
     }

     public void setBikeCompanyName(String bikeCompanyName) {
         this.bikeCompanyName = bikeCompanyName;
     }

     public SharedBike[] getSharedBikes() {
         return sharedBikes;
     }

     public void setSharedBikes(SharedBike[] sharedBikes) {
         this.sharedBikes = sharedBikes;
     }

     public Integer getSum() {
         return sum;
     }

     public void setSum(Integer sum) {
         this.sum = sum;
     }

     public Integer getCount() {
         return count;
     }

     public void setCount(Integer count) {
         this.count = count;
     }

     private Integer bikeCompanyId;// 公司id
     private String bikeCompanyName;// ofo 哈罗 摩拜
     private SharedBike[] sharedBikes;// 公司持有共享单车
     private Integer sum;//公司单车总量
     private Integer count;// 公司单车借出次数
}
class SharedBike {

    @Override
    public String toString() {
        return "SharedBike{" +
                "bid=" + bid +
                ", bname='" + bname + '\'' +
                ", status=" + status +
                ", borrowTime='" + borrowTime + '\'' +
                '}' ;
    }

    public SharedBike(){};
     public SharedBike(Integer bid, String bname,
                       Integer status, String borrowTime) {
         this.bid = bid;
         this.bname = bname;
         this.status = status;
         this.borrowTime = borrowTime;
     }

     public Integer getBid() {
         return bid;
     }

     public void setBid(Integer bid) {
         this.bid = bid;
     }

     public String getBname() {
         return bname;
     }

     public void setBname(String bname) {
         this.bname = bname;
     }

     public Integer getStatus() {
         return status;
     }

     public void setStatus(Integer status) {
         this.status = status;
     }

     public String getBorrowTime() {
         return borrowTime;
     }

     public void setBorrowTime(String borrowTime) {
         this.borrowTime = borrowTime;
     }

     private Integer bid;// 单车编号
     private String bname;// 单车名称-->ofo 哈罗 摩拜
     private Integer status;// 单车状态--> 0 已借出 1 可借状态
     private String borrowTime;// 单车借出时间
}

